﻿using System.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.NodeServices;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;

namespace DemoEf;


[ApiController]
public class TestController : Controller
{


    private TestDbContext _dbContext;

    private ILogger<TestController> _logger;

    private INodeServices nodeServices;

    public TestController(TestDbContext _dbContext, INodeServices nodeServices, ILogger<TestController> _logger)
    {
        this._dbContext = _dbContext;
        this._logger = _logger;
        this.nodeServices = nodeServices;
    }



    [Route("nodejs")]
    [HttpGet]
    public async Task<FileResult> NodeJs()
    {
        var list = new List<object>();

        list.Add(new { name = "User 1", age = 33, job = "Angular Developer" });
        list.Add(new { name = "User 2", age = 26, job = "Marketing Manager" });
        list.Add(new { name = "User 3", age = 4, job = "Big Girl" });
        list.Add(new { name = "User 4", age = 1, job = "Baby Girl" });

         var base64String = await nodeServices.InvokeAsync<string>("demo.js", "111111111111", "example2");
        var bytes = Convert.FromBase64String(base64String);

        return File(bytes, "application/pdf", "ThroughNPM.pdf");
    }




    [Route("testdb")]
    [HttpGet]
    public async Task<TbTest> Get()
    {


        //IDbContextTransaction transaction = null;

        //  transaction=  await  _dbContext.Database.BeginTransactionAsync();


        await _dbContext.BatchUpdate<TbTest>().Set(x => x.Code, "444444")
            .Where(x => string.IsNullOrEmpty(x.Name))
              .ExecuteAsync();
        //   await transaction.CommitAsync();
        // await _dbContext.TbTest.BatchUpdateAsync(x => new TbTest()
        //  {
        //      Code = "abc",Name = "nameabcd", Time = DateTime.Now
        //  });


        // var lst2= await _dbContext.TbTest.GroupBy(x => x.Code).Select(x => new
        // {
        //
        //     count=x.Count(x=>true),code= x.Key
        // }).AsNoTracking().ToListAsync();
        //
        //
        // foreach (var itm in lst2)
        // {
        //     _logger.LogInformation($" ---- {itm.code} {itm.count}");
        // }
        //
        // var test = new TbTest()
        // {
        //     Id = Guid.NewGuid().ToString(),
        //     Code = "name1111",
        //     //Name = "name",
        //     Time = DateTime.Now
        // };
        //
        //
        // _dbContext.TbTest.Add(test);
        // _dbContext.TbTest.Attach(test);
        // _dbContext.Entry(test).State = EntityState.Modified;

        //var m=await _dbContext.Set<TbTest>().AsQueryable().Where(x => x.Code == "code").ToListAsync();

        //var time=  _dbContext.TbTest.Where(x => x.Time != null).MaxAsync(x => x.Time);

        //_logger.LogInformation($"---{time}");
        //_dbContext.TbTest.Attach(test);

        // _dbContext.Entry(test).State = EntityState.Modified;

        // _dbContext.TbTest.Add(test);


        var lst = await _dbContext.TbTest.FindAsync("147f2439-0b4a-8bf1-9ef4-3a04ed9129eb");
        return await Task.FromResult(lst);
        // await _dbContext.SaveChangesAsync();







        /*var entity= await  _dbContext.TbTest.FindAsync("147f2439-0b4a-8bf1-9ef4-3a04ed9129eb");

        entity.Code = "testcode";
        
        
        _logger.LogInformation($"----{_dbContext.Entry(entity).State}"); 
        await _dbContext.SaveChangesAsync();

        return await Task.FromResult(entity);*/


    }

}